const router = require('koa-router')()
const path = require("path");
const pinyin = require('node-pinyin');

const avatarPath = path.join(path.resolve(__dirname, '../../../..'), '/public/images/avatars');
router.prefix('/user')

router.get("/getTeacher", async (ctx, next) => {
    console.log(ctx.query);
    let query = ctx.query;
    let sql = "select * from teacher join institute on teacher.institute_id = institute.institute_id limit ? offset ?";
    let res = await ctx.db.EXCUTE(sql, [1 * query.pageSize, query.pageSize * (query.pageNum - 1)])
    let sql2 = "select count(*) as count from teacher"
    let res1 = await ctx.db.EXCUTE(sql2, []);
    let total = res1[0].count;
    ctx.body = {code: 200, msg: "查询成功", data: {items: res, total}}
})

router.get("/getTeacherOptions", async (ctx, next) => {
    let sql = "select * from institute";
    let res = await ctx.db.EXCUTE(sql, []);
    let infoData = res;
    console.log(res);
    infoData = infoData.map(item => {
        return {label: item.institute_name, value: item.institute_id}
    })
    console.log(infoData);
    ctx.body = {code: "200", infoData};
});

router.post("/editTeacher" , async (ctx, next) => {
    const data = ctx.query;
    let userNameSql = 'select user_name from user where user_name = ? and user_id != ?'
    let sql = "select avatar from teacher where user_id = ?"
    let sql3 = "update user set user_name = ? where user_id = ?";
    let sql2 = "update teacher set user_name = ?, teacher_experience = ?, signature = ?, institute_id = ? where user_id = ?"
    let res4 = await ctx.db.EXCUTE(userNameSql, [data.user_name, data.user_id]);
    console.log(res4, "查询是否存在相同用户名");
    if(res4.length === 0) {
      // let res = await ctx.db.EXCUTE(sql, [data.user_id]);
      let res3 = await ctx.db.EXCUTE(sql3, [data.user_name, data.user_id]);
      let res2 = await ctx.db.EXCUTE(sql2, [data.user_name, data.teacher_experience, data.signature, data.institute_id, data.user_id]);
      ctx.upload.uploadAvatar(ctx, avatarPath, data.user_id, "teacher");
      ctx.body = {code: 200, msg: "修改成功"}
    } else {
      ctx.body = {code: 500, msg: "该用户名已存在"}
    }
})

router.post('/addTeacher', async (ctx, next) => {
  try {
    const data = ctx.query;
    // 创建学生用户名为学生姓名拼音加时间戳的后6位，为了确保用户名的唯一性， 所以说添加学生的时候就不需要进行用户名查重判定了
    const user_name = pinyin(data.teacher_name, {
      style: 'normal',
    }).join('') + String(Date.now()).slice(-6);
    // 此处的创建用户ID传入的为1
    let id = ctx.snowFlake.createSnow(1);
    let usersql = 'insert into user (user_id, user_password, user_name, user_auth) values (?,?,?,?)';
    // 默认添加的密码是123456
    let user_password = ctx.Rsa.encrypt(ctx, "123456");
    console.log(user_password, "密码");
    console.log(ctx.Rsa.decrypt(ctx, user_password), "解密之后");
    let res1 = await ctx.db.EXCUTE(usersql, [id, user_password, user_name, "teacher_per"]);
    let sql = 'insert into teacher (user_id, avatar, user_name, signature, teacher_experience, institute_id, teacher_id, teacher_name) values (?,?,?,?,?,?,?,?)'
    let res = await ctx.db.EXCUTE(sql, [id, '', user_name, data.signature, data.teacher_experience, data.institute_id, data.teacher_id, data.teacher_name]);
    ctx.upload.uploadAvatar(ctx, avatarPath, id, 'teacher');
    ctx.body = {code: 200, msg: "修改成功"}
  } catch(err) {
    console.log(err, "上面的是报错信息");
    // ctx.body = {code: 500, msg: err}
    if(err.code == "ER_DUP_ENTRY") {
      ctx.body = {code: 500, msg: "插入数据已存在,请修改"}
    }
  }
})

router.post("/deleteTeacher", async(ctx, next) => {
  let data = ctx.request.body;
  console.log(data);
  let sql = 'delete from user where user_id = ?'
  let res = ctx.db.EXCUTE(sql, [data.user_id]);
  let sql1 = 'delete from teacher where user_id = ?'
  let res1 = ctx.db.EXCUTE(sql1, [data.user_id]);


  console.log(res);

  ctx.body = {code: 200, msg: "删除成功"}
})



module.exports = router